package io.unicorn.modules.water.service.impl;

import com.alibaba.excel.EasyExcel;
import io.unicorn.common.excelutils.ExcelListener;
import io.unicorn.common.exception.RRException;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.Query;

import io.unicorn.modules.water.dao.MeterArchivesDao;
import io.unicorn.modules.water.entity.MeterArchivesEntity;
import io.unicorn.modules.water.service.MeterArchivesService;
import org.springframework.web.multipart.MultipartFile;

import static com.alibaba.druid.sql.ast.SQLPartitionValue.Operator.List;


@Service("meterArchivesService")
public class MeterArchivesServiceImpl extends ServiceImpl<MeterArchivesDao, MeterArchivesEntity> implements MeterArchivesService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key   = (String)params.get("key");
        String value = (String)params.get("value");


        IPage<MeterArchivesEntity> page = this.page(
                new Query<MeterArchivesEntity>().getPage(params),
                new QueryWrapper<MeterArchivesEntity>()
                        .like(StringUtils.isNotBlank(value) && StringUtils.isNotBlank(key), key, value)
        );

        return new PageUtils(page);
    }

    @Override
    public Integer importExcel(MultipartFile myFile) {
        /*
        String fileName = myFile.getOriginalFilename();//  获取文件名
        String suffix = myFile.getOriginalFilename().substring(myFile.getOriginalFilename().lastIndexOf(".")).toUpperCase();

        Workbook workbook = null;
        if (suffix.endsWith("XLS")) {
            try {
                workbook = new HSSFWorkbook(myFile.getInputStream());//  2003版本

            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (suffix.endsWith("XLSX")) {
            try {
                workbook = new XSSFWorkbook(myFile.getInputStream());//  2007版本
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            throw new RRException("不是excel文件"); // 文件不是Excel文件
        }


        Sheet sheet = workbook.getSheetAt(0);//workbook.getSheet("sheet1");
        int rows = sheet.getLastRowNum();
        logger.info("【rows】{}", rows);
        if (rows == 0) {
            throw new RRException("空数据表");// 数据为空 请填写数据
        }
        long startTime = System.currentTimeMillis();
        for (int i = 1; i <= rows + 1; i++) {
            Row row = sheet.getRow(i);
            if (row != null) {
                int cells = row.getLastCellNum();
                for(int c = 1; c <= cells; c ++)
                {
                    String cellValue = getCellValue(row.getCell(c));
                    logger.info(cellValue);
                }
            }
        }

        long endTime = System.currentTimeMillis();
        long totaltime = endTime - startTime;
        logger.info("【消耗时间为】{}", totaltime);  //  将近两万条数据 3秒解析完成

        return rows;*/
        return 0;
    }

    public String getCellValue(Cell cell) {
        String value = "";
        if (cell != null) {
            switch (cell.getCellType()) {
                case NUMERIC:// 数字
                    value = cell.getNumericCellValue() + " ";
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                        Date date = cell.getDateCellValue();
                        if (date != null) {
                            value = new SimpleDateFormat("yyyy-MM-dd").format(date); //  日期格式化
                        } else {
                            value = "";
                        }
                    } else {
                        //  解析cell时候 数字类型默认是double类型的 但是想要获取整数类型 需要格式化
                        value = new DecimalFormat("0").format(cell.getNumericCellValue());
                    }
                    break;
                case STRING: //  字符串
                    value = cell.getStringCellValue();
                    break;
                case BOOLEAN:   //  Boolean类型
                    value = cell.getBooleanCellValue() + "";
                    break;
                case BLANK:   // 空值
                    value = "";
                    break;
                case ERROR: // 错误类型
                    value = "非法字符";
                    break;
                default:
                    value = "未知类型";
                    break;
            }

        }
        return value.trim();
    }
}
